package main.java.com.web;

import main.java.com.service.DishService;
import main.java.com.service.impl.DishServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/*
* 用于批量启售和批量停售
* */
@WebServlet("/BatchUpdateDishStatusServlet")
public class BatchUpdateDishStatusServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }



    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        // 获取请求参数中的ids和status，用于后续的批量更新菜品状态
        String idsParam = request.getParameter("ids");
        String statusParam = request.getParameter("status");

        // 检查ids和status参数是否存在，如果存在则进行批量更新操作
        if (idsParam != null && statusParam != null) {
            // 将ids参数字符串分割并转换为Long类型的列表
            List<Long> ids = Arrays.stream(idsParam.split(",")).map(Long::parseLong).toList();
            // 将status参数字符串转换为int类型
            int status = Integer.parseInt(statusParam);

            // 创建DishService实例，用于调用批量更新菜品状态的方法
            DishService dishService = new DishServiceImpl();
            // 调用batchUpdateStatus方法执行批量更新，并获取更新结果
            int result = dishService.batchUpdateStatus(ids, status);
            // 将更新结果以字符串形式写入响应中
            response.getWriter().print(result);
        } else {
            // 如果参数不足，则返回0表示更新失败
            response.getWriter().print(0);
        }
    }
}
